package com.example.myapplication.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.myapplication.R;
import com.example.myapplication.bean.TeamDetailGsonData;
import com.example.myapplication.utils.IP;
import com.example.myapplication.utils.OkHttpUtil;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

public class TeamDetailActivity extends AppCompatActivity {

    private TextView teams_tv_teamId;
    private ImageView teams_iv_teamPic;
    private TextView teams_tv_winMath;
    private TextView teams_tv_looseMath;
    private TextView teams_tv_pingMath;
    private TextView teams_tv_score;
    private TextView teams_tv_detail;
    Context context = this;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_detail);
        initView();

        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id",1);
                } catch (JSONException e) {
                    e.printStackTrace();
                };

                JSONObject post = OkHttpUtil.post(jsonObject, "/teams/findOne");
                Gson gson = new Gson();
                final TeamDetailGsonData gsonData = gson.fromJson(post.toString(),TeamDetailGsonData.class);
                if(gsonData.isSuccess()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TeamDetailGsonData.DataBean data = gsonData.getData();
                            teams_tv_teamId.setText(data.getTeamId()+"");
                            teams_tv_winMath.setText(data.getWinMath()+"");
                            teams_tv_looseMath.setText(data.getLooseMath()+"");
                            teams_tv_pingMath.setText(data.getPingMath()+"");
                            teams_tv_score.setText(data.getScore()+"");
                            teams_tv_detail.setText(data.getDetail()+"");
                            Picasso.with(context)
                                    .load(IP.zlip + "/" + data.getTeamPic())
                                    .into(teams_iv_teamPic);
                        }
                    });

                }

            }
        }).start();
    }

    private void initView() {
        toolbar = findViewById(R.id.tb_team_detail);
        teams_tv_teamId = (TextView) findViewById(R.id.teams_tv_teamId);
        teams_iv_teamPic = (ImageView) findViewById(R.id.teams_iv_teamPic);
        teams_tv_winMath = (TextView) findViewById(R.id.teams_tv_winMath);
        teams_tv_looseMath = (TextView) findViewById(R.id.teams_tv_looseMath);
        teams_tv_pingMath = (TextView) findViewById(R.id.teams_tv_pingMath);
        teams_tv_score = (TextView) findViewById(R.id.teams_tv_score);
        teams_tv_detail = (TextView) findViewById(R.id.teams_tv_detail);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
}